﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using TarreOA.TarreOADAL;
using TarreOA.TarreOAModels;
using TarreOA.TarreOABLL;
public partial class page_FlowForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user"] == null)
        {
            Session["ShowMessage"] = "用户未登录，请重新登录!";
            Session["Backurl"] = "Login.aspx";
            Response.Redirect("~/Message.aspx");
        }
        else
        {
            TarreOA.TarreOAModels.User sucessUser = (TarreOA.TarreOAModels.User)Session["user"];
            bool userFlag = false;
            if (sucessUser.PopedomGroup.PopedomList != null)
            {
                foreach (Popedom var in sucessUser.PopedomGroup.PopedomList)
                {
                    if (var.PopedomId == 53)
                    {
                        userFlag = true;
                        break;
                    }
                }
            }
            if (!userFlag)
            {
                Session["ShowMessage"] = "用户没有该权限，请与管理员联系!";
                Session["Backurl"] = "Right.aspx";
                Response.Redirect("~/Message.aspx");
            }
        }
        if (!IsPostBack)
        {
            SqlConnection conn;
            SqlCommand cmd;
            SqlDataReader dr;
            conn = SQLManager.GetConn();
            cmd = new SqlCommand("Select id,flow_name From flow_type", conn);
            conn.Open();
            dr = cmd.ExecuteReader();
            DataList1.DataSource = dr;
            DataList1.DataBind();
            dr.Close();
            string str = "select id,form_name from flow_forms where id not in (select form_id from flow_newflow)";
            SqlDataAdapter myda = new SqlDataAdapter(str, conn);
            DataSet ds = new DataSet();
            
            myda.Fill(ds, "lis");

            GridView2.DataSource = ds;
            GridView2.DataBind();
            
    

            conn.Close();
            conn.Dispose();


        }
        
    
    }
    protected void DataList1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item ||
             e.Item.ItemType == ListItemType.AlternatingItem)
        {
            GridView g = (GridView)e.Item.FindControl("GridView1");
            HiddenField hf = (HiddenField)e.Item.FindControl("HiddenField1");
            Label formTitleLbl = (Label)e.Item.FindControl("formTitleLbl");
            
            
            Flow_FindFormByType ffbt=new Flow_FindFormByType();
            formTitleLbl.Text = ffbt.getFlowName(hf.Value).ToString();
          
            g.DataSource = ffbt.getFormsById(hf.Value);
            
            g.DataBind();
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName=="EditFormName"){

            Response.Redirect("EditFromName.aspx?id="+e.CommandArgument.ToString());

        }
        if (e.CommandName == "Show")
        {

            Response.Redirect("Flow_ShowForm.aspx?id=" + e.CommandArgument.ToString());

        }
        if (e.CommandName == "EditForm")
        {

            Response.Redirect("EditForm.aspx?id=" + e.CommandArgument.ToString());

        }
        if (e.CommandName == "export")
        {
            Flow_EditForm fef=new Flow_EditForm();

            string name = fef.getFlowFormName(e.CommandArgument.ToString());
            string filepath = Server.MapPath("./flow_forms/" + e.CommandArgument.ToString() + ".txt");
            string filename = System.IO.Path.GetFileName(filepath);
            Response.Clear();
            Response.ContentType = "application/octet-stream";
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
            //这里的filename可以输出时自定义,不一定用原来的.
            Response.TransmitFile(filepath);
            Response.Flush();
            Response.Close();
        } if (e.CommandName == "introduction")
        {

            Response.Redirect("Flow_Introduction.aspx?id=" + e.CommandArgument.ToString());

        }
        if (e.CommandName == "Delete")
        {

            Response.Redirect("Flow_DeleteForm.aspx?id=" + e.CommandArgument.ToString());

        }
    }
    //protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    //{
    //    if (e.CommandName == "EditFormName")
    //    {

    //        Response.Redirect("EditFromName.aspx?id=" + e.CommandArgument.ToString());

    //    }
    //    if (e.CommandName == "Show")
    //    {

    //        Response.Redirect("Flow_ShowForm.aspx?id=" + e.CommandArgument.ToString());

    //    }
    //    if (e.CommandName == "export")
    //    {
    //        Flow_EditForm fef = new Flow_EditForm();

    //        string name = fef.getFlowFormName(e.CommandArgument.ToString());
    //        string filepath = Server.MapPath("./flow_forms/" + e.CommandArgument.ToString() + ".txt");
    //        string filename = System.IO.Path.GetFileName(filepath);
    //        Response.Clear();
    //        Response.ContentType = "application/octet-stream";
    //        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
    //        //这里的filename可以输出时自定义,不一定用原来的.
    //        Response.TransmitFile(filepath);
    //        Response.Flush();
    //        Response.Close();
    //    } if (e.CommandName == "introduction")
    //    {

    //        Response.Redirect("Flow_Introduction.aspx?id=" + e.CommandArgument.ToString());

    //    }
    //}
    protected void Button1_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("Flow_CreateFlowForm.aspx");
    }
    protected void GridView2_RowCommand1(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "EditFormName")
        {

            Response.Redirect("EditFromName.aspx?id=" + e.CommandArgument.ToString());

        }
        if (e.CommandName == "EditForm")
        {

            Response.Redirect("EditForm.aspx?id=" + e.CommandArgument.ToString());

        }
        if (e.CommandName == "Show")
        {

            Response.Redirect("Flow_ShowForm.aspx?id=" + e.CommandArgument.ToString());

        }
        if (e.CommandName == "Delete")
        {

            Response.Redirect("Flow_DeleteForm.aspx?id=" + e.CommandArgument.ToString());

           

        }
        if (e.CommandName == "export")
        {
            Flow_EditForm fef = new Flow_EditForm();

            string name = fef.getFlowFormName(e.CommandArgument.ToString());
            string filepath = Server.MapPath("./flow_forms/" + e.CommandArgument.ToString() + ".txt");
            string filename = System.IO.Path.GetFileName(filepath);
            Response.Clear();
            Response.ContentType = "application/octet-stream";
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
            //这里的filename可以输出时自定义,不一定用原来的.
            Response.TransmitFile(filepath);
            Response.Flush();
            Response.Close();
        } if (e.CommandName == "introduction")
        {

            Response.Redirect("Flow_Introduction.aspx?id=" + e.CommandArgument.ToString());

        }
    }
}
